home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / ftpser1a / vbsock.bas < prev    next >
Encoding:
BASIC Source File  |  1999-08-28  |  11.6 KB  |  324 lines

  1. Attribute VB_Name = "VBSOCK"
  2. Option Explicit
  3.  
  4. Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, Src As Any, ByVal cb&)
  5. Declare Function lstrlen Lib "kernel32" (ByVal lpString As Any) As Integer
  6.  
  7. Global DnsHost As String
  8. Global MaxSockets As Integer
  9. Global MaxUDP As Long
  10. Global Description As String
  11. Global Status As String
  12.  
  13.  
  14. Function ConnectSocket(ByVal hWndtyp As Long, ByVal Host As String, ByVal Port As Integer) As Long
  15. Dim SockreadBuffer As String, RetIpPort As String
  16. Dim S As Long, Dummy As Long
  17. 'Dim NewSock As SockAddr
  18. Dim SelectOps As Integer
  19.     SockreadBuffer = ""
  20.     SockAddr.sin_family = AF_INET
  21.     SockAddr.sin_port = htons(Port)
  22.     If Val(SockAddr.sin_zero) = INVALID_SOCKET Then
  23.         ConnectSocket = INVALID_SOCKET
  24.         Exit Function
  25.     End If
  26.     SockAddr.sin_addr = GetHostByNameAlias(Host)
  27.     If SockAddr.sin_addr = INADDR_NONE Then
  28.         ConnectSocket = INVALID_SOCKET
  29.         Exit Function
  30.     End If
  31.     RetIpPort = GetAscIP(SockAddr.sin_addr) & ":" & ntohs(SockAddr.sin_port)
  32.     Debug.Print RetIpPort
  33.     S = Socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)
  34.     If S < 0 Then
  35.         ConnectSocket = INVALID_SOCKET
  36.         Exit Function
  37.     End If
  38.     'If SetSockLinger(S, 1, 0) = SOCKET_ERROR Then
  39.     '    If S > 0 Then
  40.     '       Dummy = closesocket(S)
  41.     '    End If
  42.     '    ConnectSocket = INVALID_SOCKET
  43.     '    Exit Function
  44.     'End If
  45.     SelectOps = FD_CONNECT Or FD_READ Or FD_WRITE Or FD_CLOSE Or FD_ACCEPT
  46.     If WSAAsyncSelect(S, hWndtyp, ByVal 5152, ByVal SelectOps) Then
  47.         If S > 0 Then
  48.             Dummy = closesocket(S)
  49.         End If
  50.         ConnectSocket = INVALID_SOCKET
  51.         Exit Function
  52.     End If
  53.     If connect(S, SockAddr, SockAddr_Size) <> -1 Then
  54.         If S > 0 Then
  55.             Dummy = closesocket(S)
  56.         End If
  57.         ConnectSocket = INVALID_SOCKET
  58.         Exit Function
  59.     End If
  60.     ConnectSocket = S
  61. End Function
  62.  
  63. Function WSAGetSelectEvent(ByVal lParam As Long) As Long
  64.     WSAGetSelectEvent = Int(lParam Mod 65536)
  65. End Function
  66.  
  67. 'Public Function WSAGetSelectEvent(ByVal lParam As Long) As Integer
  68. '    If (lParam And &HFFFF&) > &H7FFF Then
  69. '        WSAGetSelectEvent = (lParam And &HFFFF&) - &H10000
  70. '    Else
  71. '        WSAGetSelectEvent = lParam And &HFFFF&
  72. '    End If
  73. 'End Function
  74.  
  75. Public Function WSAGetAsyncError(ByVal lParam As Long) As Long
  76.     WSAGetAsyncError = (lParam And &HFFFF0000) \ &H10000
  77. End Function
  78.  
  79. Function DNS_Lookup(ByVal dnsip As String) As String
  80.   DnsHost = ""
  81.   vbWSAStartup
  82.   DoEvents
  83.   DNS_Lookup = vbGetHostByAddress(dnsip)
  84.   DoEvents
  85.   vbWSACleanup
  86. End Function
  87.  
  88. Function vbGetHostByAddress(ByVal sAddress As String) As String
  89.   Dim lAddress As Long
  90.   Dim PointerToMemoryLocation As Long
  91.   Dim HostName As String
  92.   Dim hostent As hostent
  93.   lAddress = inet_addr(sAddress)
  94.   PointerToMemoryLocation = gethostbyaddr(lAddress, 4, PF_INET)
  95.   If PointerToMemoryLocation <> 0 Then
  96.     CopyMemory hostent, ByVal PointerToMemoryLocation, Len(hostent)
  97.     HostName = String(256, 0)
  98.     CopyMemory ByVal HostName, ByVal hostent.h_name, 256
  99.     If HostName = "" Then
  100.       vbGetHostByAddress = "Unable to Resolve Address"
  101.     Else
  102.       vbGetHostByAddress = Left(HostName, InStr(HostName, Chr(0)) - 1)
  103.     End If
  104.   Else
  105.     vbGetHostByAddress = "No DNS Entry"
  106.   End If
  107. End Function
  108.  
  109. Function LoByte(ByVal wParam As Integer)
  110.   LoByte = wParam And &HFF&
  111. End Function
  112.  
  113. Function HiByte(ByVal wParam As Integer)
  114.   HiByte = wParam / &H100 And &HFF&
  115. End Function
  116.  
  117. Sub vbWSAStartup()
  118.   Dim iReturn As Integer
  119.   Dim sHighByte As String
  120.   Dim sLowByte As String
  121.   Dim sMsg As String
  122.   Dim i As Integer
  123.   iReturn = WSAStartup(&H101, WSAdata)
  124.    If LoByte(WSAdata.wVersion) < WS_VERSION_MAJOR Or _
  125.     (LoByte(WSAdata.wVersion) = WS_VERSION_MAJOR _
  126.     And HiByte(WSAdata.wVersion) < WS_VERSION_MINOR) Then
  127.         sHighByte = Trim(Str(HiByte(WSAdata.wVersion)))
  128.         sLowByte = Trim(Str(LoByte(WSAdata.wVersion)))
  129.         End
  130.     End If
  131.     If WSAdata.iMaxSockets < MIN_SOCKETS_REQD Then
  132.         sMsg = "This application requires a minimum of "
  133.         sMsg = sMsg & Trim$(Str$(MIN_SOCKETS_REQD)) & " supported sockets."
  134.         End
  135.     End If
  136.     MaxSockets = WSAdata.iMaxSockets
  137.     If MaxSockets < 0 Then
  138.         MaxSockets = 65536 + MaxSockets
  139.     End If
  140.     MaxUDP = WSAdata.iMaxUdpDg
  141.     If MaxUDP < 0 Then
  142.         MaxUDP = 65536 + MaxUDP
  143.     End If
  144.     Description = WSAdata.szDescription
  145.     Status = ""
  146.     Status = WSAdata.szSystemStatus
  147. End Sub
  148.  
  149. Sub vbWSACleanup()
  150.   Dim iReturn As Long
  151.   Dim sMsg As String
  152.   iReturn = WSACleanup()
  153.   If iReturn <> 0 Then
  154.     sMsg = "WSock32 Error - " & Trim$(Str$(iReturn)) & " occurred in Cleanup"
  155.     End
  156.   End If
  157. End Sub
  158.  
  159.  
  160. 'returns IP as long, in network byte order
  161. Public Function GetHostByNameAlias(ByVal HostName$) As Long
  162.     'Return IP address as a long, in network byte order
  163.     Dim phe&
  164.     Dim heDestHost As hostent
  165.     Dim addrList&
  166.     Dim retIP&
  167.     retIP = inet_addr(HostName$)
  168.     If retIP = INADDR_NONE Then
  169.         phe = gethostbyname(HostName$)
  170.         If phe <> 0 Then
  171.             CopyMemory heDestHost, ByVal phe, Len(heDestHost)
  172.             CopyMemory addrList, ByVal heDestHost.h_addr_list, 4
  173.             CopyMemory retIP, ByVal addrList, heDestHost.h_length
  174.         Else
  175.             retIP = INADDR_NONE
  176.         End If
  177.     End If
  178.     GetHostByNameAlias = retIP
  179. End Function
  180.  
  181.  
  182. Public Function GetAscIP(ByVal inn As Long) As String
  183.   Dim nStr&
  184.     Dim lpStr&
  185.     Dim retString$
  186.     retString = String(32, 0)
  187.     lpStr = inet_ntoa(inn)
  188.     If lpStr Then
  189.         nStr = lstrlen(lpStr)
  190.         If nStr > 32 Then nStr = 32
  191.         CopyMemory ByVal retString, ByVal lpStr, nStr
  192.         retString = Left(retString, nStr)
  193.         GetAscIP = retString
  194.     Else
  195.         GetAscIP = "255.255.255.255"
  196.     End If
  197. End Function
  198.  
  199. Public Function SetSockLinger(ByVal SockNum As Long, ByVal OnOff As Integer, ByVal LingerTime As Integer) As Long
  200.     Dim Linger As LingerType
  201.     Linger.l_onoff = OnOff
  202.     Linger.l_linger = LingerTime
  203.     If setsockopt(SockNum, SOL_SOCKET, SO_LINGER, Linger, 4) Then
  204.         Debug.Print "Error setting linger info: " & WSAGetLastError()
  205.         SetSockLinger = SOCKET_ERROR
  206.     Else
  207.         If getsockopt(SockNum, SOL_SOCKET, SO_LINGER, Linger, 4) Then
  208.             Debug.Print "Error getting linger info: " & WSAGetLastError()
  209.             SetSockLinger = SOCKET_ERROR
  210.         Else
  211.             Debug.Print "Linger is on if nonzero: "; Linger.l_onoff
  212.             Debug.Print "Linger time if linger is on: "; Linger.l_linger
  213.         End If
  214.     End If
  215. End Function
  216.  
  217. Public Function ListenForConnect(ByVal Port&, ByVal HWndToMsg&) As Long
  218. Dim S As Long, Dummy As Long
  219. Dim SelectOps As Integer
  220.  
  221.     S = Socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)
  222.     If S < 0 Then
  223.         ListenForConnect = INVALID_SOCKET
  224.         Exit Function
  225.     End If
  226.     
  227.     SockAddr.sin_family = AF_INET
  228.     SockAddr.sin_port = htons(Port)
  229.     If SockAddr.sin_port = INVALID_SOCKET Then
  230.         ListenForConnect = INVALID_SOCKET
  231.         Exit Function
  232.     End If
  233.     SockAddr.sin_addr = htonl(INADDR_ANY)
  234.     If SockAddr.sin_addr = INADDR_NONE Then
  235.         ListenForConnect = INVALID_SOCKET
  236.         Exit Function
  237.     End If
  238.  
  239.     If bind(S, SockAddr, SockAddr_Size) Then
  240.         If S > 0 Then
  241.             Dummy = closesocket(S)
  242.         End If
  243.         ListenForConnect = INVALID_SOCKET
  244.         Exit Function
  245.     End If
  246.     
  247.     If listen(S, 1) Then
  248.         If S > 0 Then
  249.             Dummy = closesocket(S)
  250.         End If
  251.         ListenForConnect = INVALID_SOCKET
  252.         Exit Function
  253.     End If
  254.     
  255.     SelectOps = FD_CONNECT Or FD_READ Or FD_WRITE Or FD_CLOSE Or FD_ACCEPT
  256.     If WSAAsyncSelect(S, HWndToMsg, ByVal 5150, ByVal SelectOps) Then
  257.         If S > 0 Then
  258.             Dummy = closesocket(S)
  259.         End If
  260.         ListenForConnect = SOCKET_ERROR
  261.         Exit Function
  262.     End If
  263.     ListenForConnect = S
  264. End Function
  265.  
  266. Function GetWSAErrorString(ByVal errnum As Long) As String
  267.     On Error Resume Next
  268.     Select Case errnum
  269.         Case 10004: GetWSAErrorString = "Interrupted system call."
  270.         Case 10009: GetWSAErrorString = "Bad file number."
  271.         Case 10013: GetWSAErrorString = "Permission Denied."
  272.         Case 10014: GetWSAErrorString = "Bad Address."
  273.         Case 10022: GetWSAErrorString = "Invalid Argument."
  274.         Case 10024: GetWSAErrorString = "Too many open files."
  275.         Case 10035: GetWSAErrorString = "Operation would block."
  276.         Case 10036: GetWSAErrorString = "Operation now in progress."
  277.         Case 10037: GetWSAErrorString = "Operation already in progress."
  278.         Case 10038: GetWSAErrorString = "Socket operation on nonsocket."
  279.         Case 10039: GetWSAErrorString = "Destination address required."
  280.         Case 10040: GetWSAErrorString = "Message too long."
  281.         Case 10041: GetWSAErrorString = "Protocol wrong type for socket."
  282.         Case 10042: GetWSAErrorString = "Protocol not available."
  283.         Case 10043: GetWSAErrorString = "Protocol not supported."
  284.         Case 10044: GetWSAErrorString = "Socket type not supported."
  285.         Case 10045: GetWSAErrorString = "Operation not supported on socket."
  286.         Case 10046: GetWSAErrorString = "Protocol family not supported."
  287.         Case 10047: GetWSAErrorString = "Address family not supported by protocol family."
  288.         Case 10048: GetWSAErrorString = "Address already in use."
  289.         Case 10049: GetWSAErrorString = "Can't assign requested address."
  290.         Case 10050: GetWSAErrorString = "Network is down."
  291.         Case 10051: GetWSAErrorString = "Network is unreachable."
  292.         Case 10052: GetWSAErrorString = "Network dropped connection."
  293.         Case 10053: GetWSAErrorString = "Software caused connection abort."
  294.         Case 10054: GetWSAErrorString = "Connection reset by peer."
  295.         Case 10055: GetWSAErrorString = "No buffer space available."
  296.         Case 10056: GetWSAErrorString = "Socket is already connected."
  297.         Case 10057: GetWSAErrorString = "Socket is not connected."
  298.         Case 10058: GetWSAErrorString = "Can't send after socket shutdown."
  299.         Case 10059: GetWSAErrorString = "Too many references: can't splice."
  300.         Case 10060: GetWSAErrorString = "Connection timed out."
  301.         Case 10061: GetWSAErrorString = "Connection refused."
  302.         Case 10062: GetWSAErrorString = "Too many levels of symbolic links."
  303.         Case 10063: GetWSAErrorString = "File name too long."
  304.         Case 10064: GetWSAErrorString = "Host is down."
  305.         Case 10065: GetWSAErrorString = "No route to host."
  306.         Case 10066: GetWSAErrorString = "Directory not empty."
  307.         Case 10067: GetWSAErrorString = "Too many processes."
  308.         Case 10068: GetWSAErrorString = "Too many users."
  309.         Case 10069: GetWSAErrorString = "Disk quota exceeded."
  310.         Case 10070: GetWSAErrorString = "Stale NFS file handle."
  311.         Case 10071: GetWSAErrorString = "Too many levels of remote in path."
  312.         Case 10091: GetWSAErrorString = "Network subsystem is unusable."
  313.         Case 10092: GetWSAErrorString = "Winsock DLL cannot support this application."
  314.         Case 10093: GetWSAErrorString = "Winsock not initialized."
  315.         Case 10101: GetWSAErrorString = "Disconnect."
  316.         Case 11001: GetWSAErrorString = "Host not found."
  317.         Case 11002: GetWSAErrorString = "Nonauthoritative host not found."
  318.         Case 11003: GetWSAErrorString = "Nonrecoverable error."
  319.         Case 11004: GetWSAErrorString = "Valid name, no data record of requested type."
  320.         Case Else:  GetWSAErrorString = "Unknown Error..."
  321.     End Select
  322. End Function
  323.  
  324.